/*
 * @Author: szx
 * @Date: 2022-04-02 19:03:17
 * @LastEditTime: 2022-04-02 19:13:31
 * @Description:
 * @FilePath: \leetcode\剑指offerII\028\028.js
 */

function Node(val, prev, next, child) {
    this.val = val;
    this.prev = prev;
    this.next = next;
    this.child = child;
}
/**
 * @param {Node} head
 * @return {Node}
 */
var flatten = function (head) {
    const st = [];
    let cur = head;
    while (cur.next && st.length) {
        if (cur.next) {
            if (cur.child) {
                st.push(cur.next);
                cur.next = cur.child;
                cur.next.prev = cur;
                cur.child = null;
            }
            cur = cur.next;
        } else if (st.length) {
            const node = st.pop();
            cur.next = node;
            node.prev = cur;
        }
    }
    return head;
};
